<?php

namespace app\common;

use GuzzleHttp\Client;
use Tencent\TLSSigAPIv2;

class Tencent
{
	
	private string $usersig;
	private string $url = 'https://console.tim.qq.com';
	/**
	 * @var mixed
	 */
	private $app_id;
	/**
	 * @var mixed
	 */
	private $identifier;
	
	private $http_client;
	
	private static $instance = null;
	
	// 禁止被实例化
	private function __construct()
	{
		$identifier = 'administrator';
		$im = config('platform.im');
		$this->usersig = (new TLSSigAPIV2($im['appid'], $im['secret']))->genUserSig($identifier);
		$this->app_id = $im['appid'];
		$this->identifier = $identifier;
		$this->http_client = new Client();
	}
	
	// 禁止clone
	private function __clone()
	{
	
	}
	
	public static function getInstance(): object
	{
		if (empty(self::$instance)) {
			self::$instance = new self();
		}
		return self::$instance;
	}
	
	
	/**
	 * 发送消息  uid不填 以管理员身份发送消息
	 */
	function send_msg($From_Account = '', $To_Account = '',$MsgBody=[]): string
	{
		$url = $this->url . '/v4/openim/sendmsg?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'SyncOtherMachine' => 1,
			'To_Account' => (string)$To_Account,
			'MsgRandom' => mt_rand(0, 4294967295),
			'MsgBody' => $MsgBody,
		
		];
		if (!empty($From_Account)) {
			$data['From_Account'] = (string)$From_Account;
		}
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	/**
	 * 导入账号
	 * @param $uid
	 * @return string
	 */
	function account_import($uid, $Nick, $FaceUrl): string
	{
		$url = $this->url . '/v4/im_open_login_svc/account_import?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'UserID' => (string)$uid,
			'Nick' => $Nick,
			'FaceUrl' => $FaceUrl
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	
	/**
	 * 设置资料
	 * @param $uid
	 * @param $ProfileItem
	 * @return mixed|string
	 */
	function portrait_set($uid, $ProfileItem): mixed
	{
		$url = $this->url . '/v4/profile/portrait_set?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'From_Account' => (string)$uid,
			'ProfileItem' => $ProfileItem,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	/**
	 * 获取资料
	 * @param $uid
	 * @param $TagList
	 * @return mixed|string
	 */
	function portrait_get($uid = [], $TagList = []): mixed
	{
		$url = $this->url . '/v4/profile/portrait_get?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'To_Account' => explode(',', implode(',', $uid)),
			'TagList' => $TagList,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#添加群成员
	function add_group_member($GroupId, $MemberList): string
	{
		$url = $this->url . '/v4/group_open_http_svc/add_group_member?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
			'MemberList' => $MemberList,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#删除群成员
	function delete_group_member($GroupId, $MemberToDel_Account): string
	{
		$url = $this->url . '/v4/group_open_http_svc/delete_group_member?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
			'MemberToDel_Account' => $MemberToDel_Account,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#删除群组
	function destroy_group($GroupId): string
	{
		$url = $this->url . '/v4/group_open_http_svc/destroy_group?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#转让群主
	function change_group_owner($GroupId, $NewOwner_Account, $From_Account): string
	{
		$url = $this->url . '/v4/group_open_http_svc/change_group_owner?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
			'NewOwner_Account' => (string)$NewOwner_Account,
			'From_Account' => (string)$From_Account,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#修改群基础资料
	function modify_group_base_info($GroupId, $Name = '', $Introduction = '', $Notification = '', $FaceUrl = '', $MaxMemberNum = '', $ApplyJoinOption = '', $MuteAllMember = '', $InviteJoinOption = '', $From_Account = ''): string
	{
		$url = $this->url . '/v4/group_open_http_svc/modify_group_base_info?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
		];
		if (!empty($Name)) {
			$data['Name'] = $Name;
		}
		if (!empty($Introduction)) {
			$data['Introduction'] = $Introduction;
		}
		if (!empty($Notification)) {
			$data['Notification'] = $Notification;
		}
		if (!empty($FaceUrl)) {
			$data['FaceUrl'] = $FaceUrl;
		}
		if (!empty($MaxMemberNum)) {
			$data['MaxMemberNum'] = $MaxMemberNum;
		}
		if (!empty($ApplyJoinOption)) {
			$data['ApplyJoinOption'] = $ApplyJoinOption;
		}
		if (!empty($MuteAllMember)) {
			$data['MuteAllMember'] = $MuteAllMember;
		}
		if (!empty($InviteJoinOption)) {
			$data['InviteJoinOption'] = $InviteJoinOption;
		}
		if (!empty($From_Account)) {
			$data['From_Account'] = (string)$From_Account;
		}
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#获取群详细资料
	function get_group_info($GroupIdList): string
	{
		$url = $this->url . '/v4/group_open_http_svc/get_group_info?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupIdList' => $GroupIdList,
		
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#创建新群
	function create_group($Owner_Account, $Name, $Notification, $InviteJoinOption, $ApplyJoinOption = '', $FaceUrl = '', $MemberList = [], $Tags = '', $Mark = ''): string
	{
		$url = $this->url . '/v4/group_open_http_svc/create_group?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'Owner_Account' => (string)$Owner_Account,
			'Type' => 'Public',
			'Name' => $Name,
			'Notification' => $Notification,
			'InviteJoinOption' => $InviteJoinOption,
		];
		
		if (!empty($FaceUrl)) {
			$data['FaceUrl'] = $FaceUrl;
		}
		if (!empty($ApplyJoinOption)) {
			$data['ApplyJoinOption'] = $ApplyJoinOption;
		}
		if (!empty($MemberList)) {
			$data['MemberList'] = $MemberList;
		}
		if (!empty($Tags)) {
			$data['AppDefinedData'][] = ['Key' => 'Tags', 'Value' => $Tags];
		}
		if (!empty($Mark)) {
			$data['AppDefinedData'][] = ['Key' => 'Mark', 'Value' => $Mark];
		}
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
	#撤回群聊消息
	function group_msg_recall($GroupId, $MsgSeqList): string
	{
		$url = $this->url . '/v4/group_open_http_svc/group_msg_recall?sdkappid=' . $this->app_id . '&identifier=' . $this->identifier . '&usersig=' . $this->usersig . '&random=' . mt_rand(0, 4294967295) . '&contenttype=json';
		$data = [
			'GroupId' => $GroupId,
			'MsgSeqList' => $MsgSeqList,
		];
		return $this->http_client
			->post($url, [
				'body' => json_encode($data)
			])->getBody()
			->getContents();
	}
	
}